<?php

require_once("../../controllers/init.php");
require_once($FILE_SYSTEM_ROOT."/controllers/authentication.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/events/Event.php");
require_once($FILE_SYSTEM_ROOT."/model/classes/events/BookingForm.php");

if(!$GLOBALS['IS_LOGGED_ON'])
{
	echo "ERROR_NOT_LOGGED_ON";
	exit;
}



$EventID=$_POST['EventID'];

$Event=new Event($EventID);

$time = $Event->getStart()->format("H:i")." - ".$Event->getEnd()->format("H:i");
if($time=="00:00 - 00:00")$time="All day event";

//get Event Type Options
$sql = "SELECT EventTypeID, EventType, IsDefault FROM EventTypes ORDER BY EventTypeID";
$database=$GLOBALS['DATABASE']->Recopy();
$database->ExecuteSQL($sql);
$TypeOptions = "";
while($row = $database->getRow())
{
//	echo "Type ID = ".$Event->getTypeID()." Row type ID = ".$row['EventTypeID']." Row default = ".$row['IsDefault']."<br>\n";
	$isSelected=((($Event->getTypeID()==0)&&($row['IsDefault']))||($Event->getTypeID()==$row['EventTypeID']));
//	$isSelected=$row['IsDefault'];
//	echo "Isselected = ".$isSelected."<br>\n";
	$TypeOptions .= "<option value=\"".$row['EventTypeID']."\" ";
	if($isSelected) $TypeOptions .= "selected ";
	$TypeOptions .= ">".$row['EventType']."</option>\n";
}


//get Locations

$arrLocations = $GLOBALS['LOCATIONS'];
$selectedLocations=$Event->getLocations();
$LocationOptions = "";
foreach($arrLocations as $key=>$location)
{
	// is selected if $location['LocationNameID'] is in any $selectedLocations[LocationNameID]
	$isSelected=false;
	foreach($selectedLocations as $selectedLocation)
	{
		if($selectedLocation==$key)
		{
			$isSelected=true;
			break;
		}
	}
	$LocationOptions .= "<option value=\"".$key."\" ";
	if($isSelected) $LocationOptions .= "selected ";
	$LocationOptions .= ">".$location['LongName']."</option>\n";
}



//get involvees
$arrInvolvees = $GLOBALS['INVOLVEES'];
$selectedInvolvees=$Event->getInvolvees();
$InvolveeOptions = "";
foreach($arrInvolvees as $key=>$involvee)
{
	// is selected if $location['LocationNameID'] is in any $selectedLocations[LocationNameID]
	$isSelected=false;
	foreach($selectedInvolvees as $selectedInvolvee)
	{
		if($selectedInvolvee==$key)
		{
			$isSelected=true;
			break;
		}
	}
	$InvolveeOptions .= "<option value=\"".$key."\" ";
	if($isSelected) $InvolveeOptions .= "selected ";
	$InvolveeOptions .= ">".$involvee['LongName']."</option>\n";
}


// find out if there is a booking form

/*$bookingForm = new BookingForm($EventID);


$isBookingForm=($bookingForm->getBookingFormID()=='')?false:true;
$bookingFormLabel = $isBookingForm?"Edit Booking Form":"Create Booking Form";

$CreateWordDocButtonHTML = $isBookingForm?"<span id=\"calendar-month-event-edit-booking-word\">Create Word Document</span>":"";

*/
//echo $Event->getCreatedBy()."<br>\n";

?>


<div id="calendar-month-event-edit-title-bar">Edit Event<div id="calendar-month-event-edit-close">X</div></div>
<div id="calendar-month-event-edit-container">
	<div id="calendar-month-event-edit-deleted"><?php if($Event->getDeletedBy()!=0)echo "Deleted by ".$USERS[$Event->getDeletedBy()]['LongName']." on ".$Event->getDeletedWhen()->format("l j F Y \a\\t H:i");?></div>
	<div id="calendar-month-event-edit-created"><?php if($Event->getCreatedBy()!=0)echo "Created by ".$USERS[$Event->getCreatedBy()]['LongName']." on ".$Event->getCreatedWhen()->format("l j F Y \a\\t H:i");?></div>
	
	<input type="hidden" id="calendar-month-event-edit-eventid" value="<?php echo $Event->getEventID();?>" />
	<input type="hidden" id="calendar-month-event-edit-originalrecordid" value="<?php echo $Event->getOriginalRecordID();?>" />
	<b>Date: </b><input type="text" id="calendar-month-event-edit-date" value="<?php echo $Event->getStart()->format("Y-m-d");?>" />
	<input id="calendar-month-event-edit-allday" type="checkbox" <?php echo ((($Event->getStart()->format("H:i"))=="00:00")&&($Event->getEnd()->format("H:i"))=="00:00"?"checked=\"checked\"":"");?> >All day</input>
	<span id="calendar-month-event-edit-times">
	<b>Start: </b><input type="text" id="calendar-month-event-edit-start" value="<?php echo $Event->getStart()->format("H:i");?>" />
	<b>End: </b><input type="text" id="calendar-month-event-edit-end" value="<?php echo $Event->getEnd()->format("H:i");?>" />
	</span>
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-title">
		<span><b>Title: </b></span>
		<input placeholder="Enter a title for the event" id="calendar-month-event-edit-title-text" type="text" value="<?php echo $Event->getTitle();?>"></input>
	</div>

	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-description">
		<span><b>Description: </b></span>
		<textarea placeholder="Enter a description of the event" id="calendar-month-event-edit-description-text"><?php echo $Event->getDescription();?></textarea>
	</div>
	
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-comment">
		<span><b>Comment: </b></span>
		<textarea placeholder="Enter any comments here" id="calendar-month-event-edit-comment-text"><?php echo $Event->getComment();?></textarea>
	</div>

	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-responsible">
		<span><b>Responsible person: </b></span>
		<textarea placeholder="Enter the name of the person responsible" id="calendar-month-event-edit-responsible-text"><?php echo $Event->getResponsible();?></textarea>
	</div>
	
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-type">
		<span><b>Event Type: </b></span>
		<select id="calendar-month-event-edit-type-text"><?php echo $TypeOptions;?></select>
	</div>
	
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-locations">
		<span><b>Locations: </b></span>
		<select size="<?php echo count($arrLocations);?>" multiple id="calendar-month-event-edit-locations-text"><?php echo $LocationOptions;?></select>
	</div>
	
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-involvees">
		<span><b>Involving: </b></span>
		<select size="<?php echo count($arrInvolvees);?>" multiple id="calendar-month-event-edit-involvees-text"><?php echo $InvolveeOptions;?></select>
	</div>

	
	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-multi">
		<b>Repeat every </b>
		<select id="calendar-month-event-edit-multi-repeatoptions">
			<option value="day">day</option>
			<option value="week" selected >week</option>
			<option value="month">month</option>
		</select>
		<b>until and including: </b><input type="text" id="calendar-month-event-edit-repeatuntil" value="<?php echo $Event->getStart()->format("Y-m-d");?>" />

	</div>

<!--	<div class="calendar-month-event-edit-item" id="calendar-month-event-edit-booking">
		<span id="calendar-month-event-edit-booking-add"><?php echo $bookingFormLabel; ?></span><?php echo $CreateWordDocButtonHTML; ?>
	</div>
-->
	<hr>
	<div id="calendar-month-event-edit-control">
		<span class="calendar-month-event-edit-buttons" id="calendar-month-event-edit-update-button">Update</span>
		<span class="calendar-month-event-edit-buttons" id="calendar-month-event-edit-insert-button">Insert as New</span>
		<span class="calendar-month-event-edit-buttons" id="calendar-month-event-edit-delete-button">Delete</span>
		<span class="calendar-month-event-edit-buttons" id="calendar-month-event-edit-cancel-button">Cancel</span>
	</div>

</div>


